package model;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class DistrDAO extends BaseDAO{
	
	// Init ---------------------------------------------------------------------------------------

    private Map<Long, Distr> distrMap = new LinkedHashMap<Long, Distr>();
    
    // Actions ------------------------------------------------------------------------------------

    /**
     * Create DAO class object  and load static map with objects of target class
     * @throws Exception
     */
    public DistrDAO() throws Exception {
    	loadAllDistributorsToMap();
    }
    
    /**
     * Look object in map
     * @param id object id
     * @return Object
     */
    public Distr find(Long id) {
        return distrMap.get(id);
    }

    /**
     * Get distributor list
     * @return
     * @throws Exception 
     */
    public List<Distr> list() throws Exception {
    	loadAllDistributorsToMap();	//refresh data
        return new ArrayList<Distr>(distrMap.values());
    }

    /**
     * Get map with distributors
     * @return
     * @throws Exception
     */
    public Map<Long, Distr> map() throws Exception {
    	loadAllDistributorsToMap(); //refresh data
        return distrMap;
    }
    
    /**
     * Add distributor
     * @param distr
     * @throws Exception
     */
    public void addDistr(Distr distr) throws Exception {
    	this.saveDistr(distr);
    }
    
    /**
     * Save distributor. If distributor already exist, than save it data
     * @param distr
     * @throws Exception
     */
    public void saveDistr(Distr distr) throws Exception {
    	boolean distrExist = false;
    	// look in map
    	for (Long distrId : distrMap.keySet()) {
    		// find distributor
			if (distr.getId() == distrId) {
				this.save(distr);
				distrMap.put(distrId, distr);
				distrExist = true;
				break;
			}
		}
    	// distributor not exist
    	if (!distrExist) {
    		this.save(distr);
    		distrMap.put(distr.getId(), distr);
    	}
    }
    
    /**
     * Delete distributor 
     * @param distr
     * @throws Exception 
     */
    public void deleteDistr(Distr distr) throws Exception {
    	this.delete(distr);
    	distrMap.remove(distr.getId());
    	System.out.println();
    }
    
    // Helpers ------------------------------------------------------------------------------------

	@Override
	protected void setStoredProcedureName() {
		this.selectStoreProcedureName = "selectDistributor";
		this.insertStoreProcedureName = "insertDistributor";
		this.updateStoreProcedureName = "updateDistributor";
		this.deleteStoreProcedureName = "deleteDistributor";
	}
	
	/*
	 * Load object data to map
	 */
	private void loadAllDistributorsToMap() throws Exception {
    	for (Object distributor : this.selecttObjectList(new Distr())) {
			distrMap.put(((Distr) distributor).getId(), (Distr) distributor);
		}
	}

}
